/**
 * 
 */
package com.johnny.xia.filetest;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.johnny.xia.util.file.AppendToFile;

/**
 * @author johnnyxia
 *
 */
public class LogFile{
    
   static String logTime = "2017-01-01 ";
    
    public static void main(String[] args){
        
        for(int i=1;i<=4;i++){
            String file = "daily.log.2017-01-03.log";
            String filePath="daily.log.2017-01-03_"+i+".log\\";
            String path = "C:\\mclog\\20170103\\app\\"+filePath;
            String path2 = "C:\\mclog\\20170103\\bak\\"+filePath;
            String fileStr = readFileByLines(path+file);
            
            File dir = new File(path2);
            if(!dir.exists()){
                dir.mkdirs();
            }
            
            AppendToFile.appendMethodA(path2+file, fileStr);  
        }
//        String file = "catalina.out-20170101";
//        String filePath="catalina.out-20170101_3\\";
//        String path = "D:\\mclog\\20170102\\"+filePath;
//        String path2 = "D:\\mclog\\20170102\\20170101_log\\"+filePath;
//        String fileStr = readFileByLines(path+file);
////        fw(path2+file, fileStr);
//        AppendToFile.appendMethodA(path2+file, fileStr);
        
//        String s = "    at org.owasp.esapi.reference.validation.StringValidationRule.checkLength(StringValidationRule.java:214)";
        
//        System.out.println(s.startsWith(" "));
//        System.out.println(isok(" atsdfsdfsdf"));
        
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static String readFileByLines(String fileName){
        System.out.println(fileName);
        
        StringBuffer buffer = new StringBuffer();

        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null){
                if(!isok(tempString)){
                    continue;
                }
                buffer.append(splitStr(tempString)).append("\r\n");
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        return buffer.toString();
    }
    
    public static boolean isok(String str){
        
        if(str.contains("Exception")){
            return false;
        }
        if(str.contains("Log4JLogger.java:449")){
            return false;
        }
        
        if(str.length() < 8){
            return false;
        }
        
       /* String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;*/
        
        str = str.replace(logTime, "");
        
        String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;
    }
    
    public static String splitStr(String str){
        if(str.contains("PaymentController.java:472")){
            return str.split("\\,address")[0];
        }else if(str.contains("PaymentController.java:197")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:214")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:223")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }
        return str;
    }
    
    public static void fw(String file ,String s){
        FileOutputStream out = null;   

        try {   

            out = new FileOutputStream(new File(file));   
                out.write(s.getBytes());   
            out.close();   


        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
